package dcp.bss.modules.bussiness.service.impl;

import dcp.bss.modules.bussiness.entity.DeviceOprLogEntity;
import dcp.bss.modules.bussiness.service.DeviceOprLogService;
import dcp.bss.modules.sys.entity.SysUserEntity;
import dcp.bss.web.exception.RRException;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
 import java.util.List;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import dcp.bss.common.util.PageUtils;
import dcp.bss.web.util.Query;
import dcp.bss.query.QueryViewVo;
import dcp.bss.modules.bussiness.dao.LockStatusDao;
import dcp.bss.modules.bussiness.entity.LockStatusEntity;
import dcp.bss.modules.bussiness.service.LockStatusService;
import org.springframework.transaction.annotation.Transactional;


@Service("baseMapper")
public class LockStatusServiceImpl extends ServiceImpl<LockStatusDao, LockStatusEntity> implements LockStatusService {
    @Autowired
    private DeviceOprLogService deviceOprLogService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<LockStatusEntity> page = this.selectPage(
                new Query<LockStatusEntity>(params).getPage(),
                new EntityWrapper<LockStatusEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(QueryViewVo<LockStatusEntity> queryViewVo) {
        Page<LockStatusEntity> page = this.selectPage(queryViewVo.getPageUtil(), queryViewVo.getWrapper());
        return new PageUtils(page);
    }

    @Override
    public List<LockStatusEntity> download(QueryViewVo<LockStatusEntity> queryViewVo) {
        List<LockStatusEntity> list = this.selectList(queryViewVo.getWrapper());
        return list;
    }

    @Override
    @Transactional
    public void unlock(String lockNum) {
        SysUserEntity sysUserEntity =     (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        LockStatusEntity lockStatusEntity = this.selectOne(new EntityWrapper<LockStatusEntity>().eq("lock_code", lockNum));
        if (lockStatusEntity==null) {
            throw new RRException(lockNum + "不存在,请检查输入的锁编号");
        }
        LockStatusEntity lockStatus = new LockStatusEntity();
        lockStatus.setLockStatus("解封");
        baseMapper.update(lockStatus, new EntityWrapper<LockStatusEntity>().eq("lock_code", lockNum));
        DeviceOprLogEntity deviceOprLog = new DeviceOprLogEntity();
        deviceOprLog.setDeviceCode(lockNum);
        deviceOprLog.setDeviceId(lockStatusEntity.getId());
        deviceOprLog.setOprator(sysUserEntity.getIpName());
        deviceOprLog.setOprTm(new Date());
        deviceOprLog.setOprType("解封");
        deviceOprLogService.insert(deviceOprLog);
        
    }

    @Override
    @Transactional
    public void lock(String lockNum) {

        SysUserEntity sysUserEntity =     (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        LockStatusEntity lockStatusEntity = this.selectOne(new EntityWrapper<LockStatusEntity>().eq("lock_code", lockNum));
        if (lockStatusEntity==null) {
            throw new RRException(lockNum + "不存在,请检查输入的锁编号");
        }
        LockStatusEntity lockStatus = new LockStatusEntity();
        lockStatus.setLockStatus("锁定");
        baseMapper.update(lockStatus, new EntityWrapper<LockStatusEntity>().eq("lock_code", lockNum));
        DeviceOprLogEntity deviceOprLog = new DeviceOprLogEntity();
        deviceOprLog.setDeviceCode(lockNum);
        deviceOprLog.setDeviceId(lockStatusEntity.getId());
        deviceOprLog.setOprator(sysUserEntity.getIpName());
        deviceOprLog.setOprTm(new Date());
        deviceOprLog.setOprType("锁定");
        deviceOprLogService.insert(deviceOprLog);
    }

}
